/*
 * This file is part of the EasyLogger Library.
 *
 * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Function: Portable interface for RT-Thread.
 * Created on: 2015-04-28
 */

#include "elog.h"
#include <time.h>
#include <string.h>
#include <stdio.h>

#define log_d(...) elog_d("elog.output.d", __VA_ARGS__)
/* Identifier of sync object */
#define _ELOG_TIMEOUT   100
_ELOG_SYNC_t	sobj;

/**
 * EasyLogger port initialize
 *
 * @return result
 */
ElogErrCode elog_port_init(void) {
    ElogErrCode result = ELOG_NO_ERR;

    return result;
}

/**
 * output log port interface
 */
void elog_port_output(const char *output, size_t size) {
    /* output to terminal */
    printf(output);
    //TODO output to flash
}

/**
 * get current time interface
 *
 * @return current time
 */
const char *elog_port_get_time(void) {
  time_t currentTime;
  struct tm *localTime;
  static char cur_system_time[21] = { 0 };
  time(&currentTime);
  localTime = localtime(&currentTime);
  strftime(cur_system_time, sizeof(cur_system_time), "%Y-%m-%d %H:%M:%S", localTime);
  return cur_system_time;
}

/**
 * get current process name interface
 *
 * @return current process name
 */
const char *elog_port_get_p_info(void) {
    return "";
}

/**
 * get current thread name interface
 */
const char *elog_port_get_t_info(void) {
    return "";
}
/*------------------------------------------------------------------------*/
// communication with the device stream
/*------------------------------------------------------------------------*/
/**
 * open then output stream device
 */
ElogErrCode elog_port_open()
{
  ElogErrCode res = ELOG_ERR;

  res = ELOG_NO_ERR;

  return res;
}
/**
 * write data to the stream device
 */
ElogErrCode elog_port_write( char *buf, size_t len )
{
  ElogErrCode res = ELOG_ERR;

  res = ELOG_NO_ERR;

  
  return res;
}
/**
 * close then output stream device
 */
ElogErrCode elog_port_close()
{
  ElogErrCode res = ELOG_ERR;
  res = ELOG_NO_ERR;
  return res;
}

/*------------------------------------------------------------------------*/
// four funcs about Synchronization Object
/*------------------------------------------------------------------------*/
/* Create a sync object */
int  elog_port_cre_syncobj (_ELOG_SYNC_t* sobj)
{
  int ret;

  ret = (int)(*sobj != 0);

  return ret;
}
/* Lock sync object */
int  elog_port_req_grant   (_ELOG_SYNC_t sobj)
{
  int ret;

  ret = 0x00;
  return ret;
}
/* Unlock sync object */
void elog_port_rel_grant   (_ELOG_SYNC_t sobj)
{

}
/* Delete a sync object */
int  elog_port_del_syncobj (_ELOG_SYNC_t sobj)
{
  int ret;

  ret = 1;

  return ret;
}